﻿using Hrm.Core;
using Hrm.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Hrm.Web.Areas.Studio.Controllers
{
    public class PortfolioController : Controller
    {
        IPortfolioService _portfolioService;
        IDictionaryService _dictionaryService;
        public PortfolioController(IPortfolioService portfolioService, IDictionaryService dictionaryService)
        {
            this._portfolioService = portfolioService;
            this._dictionaryService = dictionaryService;
        }

        private void Init()
        {
            var list = this._portfolioService.GetPortfolioTypes().Select(p => new SelectListItem()
            {
                Text = p.Name,
                Value = p.Code,
            }).ToList();
            list.Insert(0, new SelectListItem()
            {
                Text = "请选择",
                Value = "",
                Selected = true
            });
            ViewBag.PortfolioType = list;
        }

        public ActionResult Index()
        {
            var selectList=this._portfolioService.GetPortfolioTypes().Select(p => new SelectListItem() { Value=p.Code,Text=p.Name}).ToList();
            selectList.Insert(0,new SelectListItem(){Selected=true,Text="请选择"});
            ViewBag.PortfolioType = selectList;
            var result = this._portfolioService.GetPortfolios();
            return View(result);
        }

        public ActionResult Create()
        {
            var model = new PortfolioDto();
            Init();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(PortfolioDto model)
        {
            HttpPostedFileBase file = Request.Files[0];
            var filepath = Server.MapPath("~/Images/upload/Portfolio/") + DateTime.Now.ToString("yyyyMMddhhmmssffff") + file.FileName;
            model.ImageUrl = "/Images/upload/Portfolio/" + DateTime.Now.ToString("yyyyMMddhhmmssffff") + file.FileName;
            file.SaveAs(filepath);

            this._portfolioService.AddPortfolio(model);
            return RedirectToAction("Index");
        }

        public ActionResult AddPortfolioType()
        {
            var type = new PortfolioTypeDto();
            return View(type);
        }

        [HttpPost]
        public ActionResult AddPortfolioType(PortfolioTypeDto req)
        {
            if(ModelState.IsValid)
            {
                this._portfolioService.AddPortfolioTypes(req);
                return RedirectToAction("Create");
            }
            return View(req);
        }

        public ActionResult Edit(int id)
        {
            Init();
            var model = this._portfolioService.GetPortfolio(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(PortfolioDto model)
        {
            if (!string.IsNullOrWhiteSpace(Request.Files[0].FileName))
            {
                HttpPostedFileBase file = Request.Files[0];
                    var filepath = Server.MapPath("~/Images/upload/Portfolio/") + DateTime.Now.ToString("yyyyMMddhhmmssffff") + file.FileName;
                    model.ImageUrl = "/Images/upload/Portfolio/" + DateTime.Now.ToString("yyyyMMddhhmmssffff") + file.FileName;
                    file.SaveAs(filepath);
                
            }
            else
            {
                model.ImageUrl = string.Empty;
            }
            

            this._portfolioService.UpdatePortfolio(model);
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id,int imageId)
        {
            this._portfolioService.DeletePortfolio(id,imageId);
            return RedirectToAction("Index");
        }
    }
}